import { Component, OnInit } from '@angular/core';
import {FormBuilder, FormGroup, Validators} from '@angular/forms';
import {Category} from '../../../../../domain/Category';
import { UploadXHRArgs } from 'ng-zorro-antd';
import {UploadApiService} from '../../../../services/upload/upload-api.service';
import {CateApiService} from '../../../../services/cate/cate-api.service';
import {Router} from '@angular/router';

@Component({
  selector: 'app-cate-create',
  templateUrl: './cate-create.component.html',
  styleUrls: ['./cate-create.component.less']
})
export class CateCreateComponent implements OnInit {

  validateForm: FormGroup;
  cate: Category = new Category();
  coverPic: string = '';

  constructor(private fb: FormBuilder, private upload: UploadApiService, private cateApi: CateApiService, private router: Router) { }

  ngOnInit() {
    this.validateForm = this.fb.group({
      name            : [ null, [ Validators.required ] ],
      summary         : [ null, [ Validators.required ] ],
      order           : [ null, [ Validators.required ] ],
    });
  }

  submitForm(): void {
    for (const i in this.validateForm.controls) {
      this.validateForm.controls[ i ].markAsDirty();
      this.validateForm.controls[ i ].updateValueAndValidity();
    }
    this.cate.coverPic = this.coverPic;
    console.log(this.cate);

    this.cateApi.create(this.cate).subscribe((response) => {
      if (response.success) {
          this.router.navigate(['/home/cate/list'])
      }
    })
  }

  fileReq = (item: UploadXHRArgs) => {
    return this.upload.upload(item,(body) => {
      console.log(body);
      this.coverPic = body.data;
    })
  }


}
